"""
    Author:
    Date: 2018-12-11

    redis管理类
"""

# import datetime
import json
import time

from utils import db
# from utils import util


KEY_PARTNER_PROMOTE_IP = 'download:%s'
# 用户的查询历史记录
KEY_SEARCH_HISTORY_FOR_OPENID = 'search:history:for:%s'
# 每天的在线用户列表(zset: uid = > timestamp)
KEY_ONLINE_USERS_PER_DAY = 'online:users:at:%s'
# 缓存用户的充值IP
KEY_USER_PAY_IP = 'pay:ip:user:%d'

def get_cached_inviter_id_by_ip(ip):
    """ 获取指定ip对应的邀请者id """
    key = KEY_PARTNER_PROMOTE_IP % ip
    inviter_id = db.redis_get(key)
    return None if inviter_id is None else int(inviter_id)


def cache_inviter_id_with_ip(ip, inviter_id):
    """ 缓存指定ip对应的邀请者id """
    key = KEY_PARTNER_PROMOTE_IP % ip
    db.redis_set(key, str(inviter_id))
    db.redis_expire(key, 2*24*3600)
    return True


def is_valid_gma_token(uid, gma_token):
    """ 检测用户的gma token是否有效 """
    key = f'gma:token:{uid}'
    cached_gma_token = db.redis_get(key)
    return gma_token == cached_gma_token


def cache_search_value(openid, search_value):
    """ 缓存查询记录 """
    key = KEY_SEARCH_HISTORY_FOR_OPENID % openid
    score = int(time.time())
    return db.redis_zadd(key, score, search_value)


def get_search_history(openid, count):
    """ 获取查询历史记录 """
    key = KEY_SEARCH_HISTORY_FOR_OPENID % openid
    return db.redis_zrevrange(key, 0, count-1)


class AgentPromoteCache(object):
    """ 缓存代理推广信息 """
    KEY_AGENT_PROMOTE_INFO_BY_IP = 'download:agent:info:ip:%s'

    @staticmethod
    def get_cached_inviter_info_by_ip(ip):
        """ 获取指定ip对应的代理邀请者信息 """
        key = AgentPromoteCache.KEY_AGENT_PROMOTE_INFO_BY_IP % ip
        inviter_info = db.redis_get(key)
        return inviter_info

    @staticmethod
    def cache_inviter_info_with_ip(ip, agent_id, agent_uid):
        """ 缓存指定ip对应的代理邀请者的uid """
        key = AgentPromoteCache.KEY_AGENT_PROMOTE_INFO_BY_IP % ip
        agent_info = json.dumps({
            'agent_id': agent_id,
            'agent_uid': agent_uid,
        }, ensure_ascii=False).encode('utf-8')
        db.redis_set(key, agent_info)
        db.redis_expire(key, 2*24*3600)
        return True


class AdPromoteCache(object):
    """ 广告推广信息 """
    # 缓存键值
    KEY_AD_PROMOTE_INFO_BY_IP = 'ad:promote:ip:%s'
    # 缓存时间
    KEY_AD_PROMOTE_INFO_EXPIRE_SECONDS = 2*24*3600

    @staticmethod
    def get_cached_ad_id_by_ip(ip):
        """ 获取指定ip对应的AD id """
        key = AdPromoteCache.KEY_AD_PROMOTE_INFO_BY_IP % ip
        ad_id = db.redis_get(key)
        return ad_id

    @staticmethod
    def cache_ad_id_with_ip(ip, ad_id):
        """ 缓存指定ip对应的AD id """
        key = AdPromoteCache.KEY_AD_PROMOTE_INFO_BY_IP % ip
        db.redis_set(key, ad_id)
        db.redis_expire(key, AdPromoteCache.KEY_AD_PROMOTE_INFO_EXPIRE_SECONDS)
        return True

# def get_online_users_count():
#     """ 获取当前的在线人数 """
#     today = util.datetimetostr(datetime.date.today(), '%Y%m%d')
#     key = KEY_ONLINE_USERS_PER_DAY % today
#     return { 'online_user_count': 2 }


def cache_string(key, value, ex):
    """ 缓存字符串 """
    return db.redis_set(key, value, ex)


def get_cached_string(key):
    """ 获取缓存的字符串 """
    return db.redis_get(key)

if __name__ == '__main__':
    print('ok')
